<?php

require_once ROOT . 'server/classes/arquitetura/dao/AbstractDao.class.php';

class CategoriaDao extends AbstractDao {

    const TABLE_NAME = 'categoria';

    private $entity;
    private $nome;

    /**
     * Construtor padrao.
     */
    public function __construct($entity) {
        parent::__construct();
        $this->setEntity($entity);
    }

    public function setEntity($entity) {
        $this->entity = $entity;
    }

    public function getEntity() {
        return $this->entity;
    }

    /**
     * Prepare variables to be used in sql command.
     */
    private function prepareVariables() {
        $this->nome = Util::isNotEmpty($this->entity->getNome()) ? "'" . $this->entity->getNome() . "'" : 'NULL';
    }

    /**
     * Execute the insert command on the table: categoria.
     * @return int Number of affected rows.
     */
    public function getInsertSQL() {
        $this->prepareVariables();
        $sql = 'INSERT INTO ' . self::TABLE_NAME . ' (nome, ativo, insert_date) VALUES (' . $this->nome . ', \'S\', now())';
        if ($this->log->isDebugEnabled()) {
            $this->log->logDebug($sql);
        }
        return $sql;
    }

    /**
     * Execute the update command on the table categoria.
     * @return int Number of affected rows.
     */
    public function getUpdateSQL() {
        $this->prepareVariables();
        $sql = 'UPDATE ' . self::TABLE_NAME . ' SET nome = ' . $this->nome . ', update_date = now() WHERE ID = ' . $this->entity->getId();
        if ($this->log->isDebugEnabled()) {
            $this->log->logDebug($sql);
        }
        return $sql;
    }

    /**
     * Execute the delete command on the table categoria.
     * @return int Number of affected rows.
     */
    public function getDeleteSQL() {
        if ($this->entity->getId()) {
            $sql = 'DELETE FROM ' . self::TABLE_NAME . ' WHERE ID = ' . $this->entity->getId();
            if ($this->log->isDebugEnabled()) {
                $this->log->logDebug($sql);
            }
            return $sql;
        } else {
            //Nothing to remove.
            return 0;
        }
    }

    /**
     * Execute the logical delete command on the table categoria whitch one is just an update.
     * @return int Number of affected rows.
     */
    public function getLogicalDeleteSQL() {
        if ($this->entity->getId()) {
            $sql = 'UPDATE ' . self::TABLE_NAME . ' set ativo = null WHERE ID = ' . $this->entity->getId();
            return $sql;
        } else {
            //Nothing to remove.
            return 0;
        }
    }

    /**
     * Execute the select command on the table categoria. To retrive rows.
     * @param String $where
     * @return Array of rows.
     */
    public function getFindSQL($where='') {
        $sql  = 'SELECT id, nome, insert_date, update_date FROM ' . self::TABLE_NAME .  ' where 1=1 ';
        if(Util::isNotEmpty($where)) {
            $sql .= 'and ' . $where;
        }
        
        $sql .= ' and ativo = \'S\'';
        
        if ($this->log->isDebugEnabled()) {
            $this->log->logDebug($sql);
        }
        return $sql;
    }

    /**
     * Execute the select command on the table categoria. To retrive rows no null values
     * @param String $where
     * @return Array of rows.
     */
    public function getFindNoNullSQL($where='') {
        if(Util::isNotEmpty($where)) {
            $where = ' and ' . $where;
        }
        $sql  = 'SELECT IFNULL(id, \'\') id, IFNULL(nome, \'\') nome, IFNULL(insert_date, \'\') insert_date, IFNULL(update_date, \'\') update_date FROM ' . self::TABLE_NAME;
        $sql .= ' where ativo = \'S\'' . $where;
        
        if ($this->log->isDebugEnabled()) {
            $this->log->logDebug($sql);
        }
        return $sql;
    }
    
    /**
     * Execute the select command on the table categoria. To retrive total rows
     * @param String $page
     * @param String $start
     * @param String $limit
     * @param String $where
     * @return Array of rows.
     */
    public function getNumLinhasSQLPesquisa($where='') {
        if(Util::isNotEmpty($where)) {
            $where = 'and ' . $where;
        }
        $sql  = 'select count(*) as num_rows from categoria where ativo = \'S\' ' . $where;
        return $sql;
    }
    
    /**
     * Execute the select command on the table categoria. To retrive rows no null values
     * @param String $page
     * @param String $start
     * @param String $limit
     * @param String $where
     * @return Array of rows.
     */
    public function getSQLPesquisa($start, $limit, $where='') {
        if(Util::isNotEmpty($where)) {
            $where = ' and ' . $where;
        }
        $sql  = 'SELECT id, '
                     . 'IFNULL(nome, \'\') nome, '
                     . 'IFNULL(insert_date, \'\') insert_date, '
                     . 'IFNULL(update_date, \'\') update_date '
              . 'FROM ' . self::TABLE_NAME
              . ' where ativo = \'S\'' . $where;
        
        if(Util::isEmpty($start) && Util::isEmpty($limit)) {
            $sql .= ' order by id desc LIMIT ' . IApplicationConfig::NUM_ROWS_GRID;
        } else {
            $sql .= ' order by id desc LIMIT ' . $start . ', ' . $limit;
        }
        
        if ($this->log->isDebugEnabled()) {
            $this->log->logDebug($sql);
        }
        return $sql;
    }

    /**
     * Execute the select command on the table categoria. To retrive rows no null values Using entitys values. 
     */
    public function getByEntityFindNoNullSQL() {
        $where = '1 = 1';
        $where .= Util::isNotEmpty($entity->getNome()) ? ' AND nome = \'' . trim($entity->getNome()) . '\'' : '';
        $where .= Util::isNotEmpty($entity->getAtivo()) ? ' AND ativo = \'' . trim($entity->getAtivo()) . '\'' : '';
        $where .= Util::isNotEmpty($entity->getUpdateDate()) ? ' AND update_date = \'' . trim($entity->getUpdateDate()) . '\'' : '';
        return $this->getFindNoNullSQL($where);
    }
    
    /**
     * Execute the comand inser when the id is null and an update comand when the id is informed.
     */
    public function getSalvarSQL() {
        $sql = 'call sCategoriaSalvar()' . $this->nome;
        return $sql;
    }
}

?>
